// pages/project/projectInfo/projectInfo.js
var app = getApp()
import ProjectUtils from '../../../utils/projectUtil'
import CensorTextUtil from '../../../utils/CensorTextUtil'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "编辑项目",
    projectInfo: {
      project_name: null
    },
    projectId: null
  },
  projectNameChange(e) {
    this.setData({
      projectInfo: {
        id: this.data.projectId,
        project_name: e.detail.value,
      }
    })
  },
  /**
   * 保存项目
   * @param {*} e 
   */
  SaveProject: async function (e) {
    this.ContentValidation().then(res => {
      if (res) {
        if (app.globalData.isAddProject) {
          ProjectUtils.addProject(this.data.projectInfo.project_name, res => {
            wx.navigateBack({
              changed: true
            });
          });
        } else {
          ProjectUtils.updateProject(this.data.projectInfo.id, this.data.projectInfo.project_name, res => {
            wx.navigateBack({
              changed: true
            });
          });
        }
      }
    })
  },
  async ContentValidation() {
    const doc = await new Promise((resolve) => {
      CensorTextUtil.CensorText(this.data.projectInfo.project_name).then(res => {
        if (!res) {
          this.setData({
            projectInfo: {
              id: this.data.projectInfo.id,
              project_name: null
            }
          })
          wx.showToast({
            // 提示内容
            title: "存在违规内容，请重新输入",
            icon: "none",
            duration: 2000,
            mask: false,
          })
          return resolve(false);
        } else {
          return resolve(true);
        }
      })
    })
    return doc;
  },
  /**
   * 返回
   */
  Back: function (params) {
    wx.navigateBack({
      changed: true
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.isAddProject) {
      this.setData({
        title: "新增项目",
        projectInfo: {
          project_name: null,
        }
      })
    } else {
      this.setData({
        title: "编辑项目",
        projectInfo: app.globalData.projectInfo,
        projectId: app.globalData.projectInfo.id
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})